package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.ViewPager;

import com.foolishbird.sdk.ui.activity.UserLoginActivity;
import com.foolishbird.sdk.ui.assist.LoginTypeSwitchAdapter;
import com.foolishbird.sdk.ui.assist.LoginTypeSwitchWatcher;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.ui.view.ExViewPager;


public class FoolishBirdLoginFragment extends BaseDialogFragment implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private static final String INIT_LOGIN_TYPE = "init_login_type";

//    private ImageButton mCloseDialogBtn;
//    private Button mOneKeyLoginBtn;
    private ExViewPager mLoginTypeViewPager;

//    private LoginTypeSwitchWatcher mLoginTypeSwitchWatcher;

    public static final FoolishBirdLoginFragment newInstance(int loginType) {
        FoolishBirdLoginFragment fragment = new FoolishBirdLoginFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(INIT_LOGIN_TYPE, loginType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("fb_fragment_login"));

//        TextView masterTextView = contentView.findViewById(getViewIdentifier("activity_login_title_text"));
//        TextView slaveTextView = contentView.findViewById(getViewIdentifier("activity_login_switch_text"));
//        mLoginTypeSwitchWatcher = new LoginTypeSwitchWatcher(masterTextView, slaveTextView);
//        mLoginTypeSwitchWatcher.setOnLoginTypeSwitchChangeListener(this);

//        mOneKeyLoginBtn = contentView.findViewById(getViewIdentifier("activity_login_onekey_register_btn"));
//        mCloseDialogBtn = contentView.findViewById(getViewIdentifier("activity_login_finish_btn"));
        mLoginTypeViewPager = contentView.findViewById(getViewIdentifier("activity_login_type_viewpager"));
        mLoginTypeViewPager.setGestureSlideEnable(false);

        mLoginTypeViewPager.addOnPageChangeListener(this);
//        boolean closePhoneLogin = HttpEngineApi.getInstance().getEngineConduits().readClosePhoneLogin();
//        if(closePhoneLogin){
//            mLoginTypeViewPager.setAdapter(new LoginTypeSwitchSingleAdapter(getChildFragmentManager()));
//            masterTextView.setText(ResUtils.getString(getContext(), "hnssn_login_switch_account"));
//            contentView.findViewById(getViewIdentifier("activity_login_switch_icon")).setVisibility(View.GONE);
//            slaveTextView.setVisibility(View.GONE);
//        }else{
            mLoginTypeViewPager.setAdapter(new LoginTypeSwitchAdapter(getChildFragmentManager(),this));
//        }
        mLoginTypeViewPager.setCurrentItem(getNotNullArguments().getInt(INIT_LOGIN_TYPE, LoginTypeSwitchWatcher.SWITCH_MODE_WHICH_ACCOUNT));

//        mOneKeyLoginBtn.setOnClickListener(this);
//        mCloseDialogBtn.setOnClickListener(this);
//
//        mOneKeyLoginBtn.setVisibility(WanSDK.getInstance().isShowQuickRegister() ? View.VISIBLE : View.GONE);

        return contentView;
    }

    public void setItem(int index){
        mLoginTypeViewPager.setCurrentItem(index);

        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof UserLoginActivity) {
                ((UserLoginActivity) activity).setCurrentLoginType(index);
            }
        }
    }

    private OnUserActionListener getOnUserActionListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnUserActionListener) {
                return (OnUserActionListener) activity;
            }
        }
        return null;
    }

    @Override
    public void onClick(View view) {
//        if (view == mCloseDialogBtn) {
//            finishActivity();
//        } else if (view == mOneKeyLoginBtn) {
//            mOneKeyLoginBtn.setEnabled(false);
//            mOneKeyLoginBtn.setClickable(false);
//            nextToQuickRegisterActivity();
//        }
    }

    private void nextToQuickRegisterActivity() {
        OnUserActionListener listener = getOnUserActionListener();
        if (listener != null) {
            listener.doNextToOneKeyLogin();
        }
    }

    private void finishActivity() {
        OnUserActionListener listener = getOnUserActionListener();
        if (listener != null) {
            listener.doFinishActivity(this);
        }
    }



    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
//        if (mLoginTypeSwitchWatcher != null) {
//            mLoginTypeSwitchWatcher.setCurrentLoginType(position);
//        }

        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof UserLoginActivity) {
                ((UserLoginActivity) activity).setCurrentLoginType(position);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public interface OnUserActionListener extends OnFinishActivityListener {

        void doSubmitLoginInfo(String username, String password);

        void doSubmitCellPhoneNumber(String cellphone, String verify);

        void doNextToOneKeyLogin();

        void doNextToQuickRegister();

        void doNextToChangePassword(String username);


    }

}
